﻿using System;

namespace h31
{
    public class Solution
    {
        public int EraseOverlapIntervals(int[][] intervals)
        {

            if (intervals.Length == 0) return 0;

            Array.Sort(intervals, (int[] a, int[] b) =>
            {
                if (a[0] > b[0])
                {
                    return 1;
                }

                if (a[0] < b[0])
                {
                    return -1;
                }

                return 0;
            });

            int result = 0;
            int[] current = intervals[0];
            int[] next;
            int len = intervals.Length;
            for (int i = 1; i < len; i++)
            {
                next = intervals[i];
                if (next[0] >= current[1])
                {
                    current = next;
                }
                else
                {
                    result++;
                    current = next[1] < current[1] ? next : current;
                }
            }

            return result;
        }
    }

}